package com.example.kyyl.activity;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.kyyl.R;
import com.example.kyyl.http.HttpManager;
import com.example.kyyl.http.SPUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import rx.Subscriber;

public class SanresultActivity extends AppCompatActivity {
    private BluetoothAdapter mBluetoothAdapter;// 蓝牙设备适配器
    private TextView hzname,hzchuangwei,hzroomname;
    private ImageView back;
    private Button startceliang;
    private String address="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanresult);
        hzname=findViewById(R.id.hzname);
        hzchuangwei=findViewById(R.id.hzchuangwei);
        hzroomname=findViewById(R.id.hzroomname);
        back=findViewById(R.id.back);
        startceliang=findViewById(R.id.startceliang);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();// 获取系统的蓝牙管理器
        String address=SPUtil.getString(SanresultActivity.this,"ipaddress","");
        Log.e("111111",address);
        if (address.equals("")|address==null| TextUtils.isEmpty(address)){
            Toast.makeText(SanresultActivity.this,"请在首页设置服务器地址中设置服务器地址",Toast.LENGTH_LONG).show();
        }else {
            HashMap<String,String> map=new HashMap<>();//网络通信获取患者信息
            map.put("imei", SPUtil.getString(SanresultActivity.this,"bedid",""));
            HttpManager.getInstance().get(address+"/api/get_patient_by_device", map, new Subscriber<String>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(String s) {
                    Log.e("打印返回的json======", s);
                    JSONObject jsonObject= null;
                    try {
                        jsonObject = new JSONObject(s);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data");
                        JSONObject jsonObject2=jsonObject1.getJSONObject("bed");
                        JSONObject jsonObject3=jsonObject2.getJSONObject("patient");
                        if (jsonObject2.getInt("patientId")!=0){
                            String name=jsonObject3.getString("name");
                            hzname.setText(name);
                        }else {
                            Toast.makeText(SanresultActivity.this,"无患者信息",Toast.LENGTH_LONG).show();
                        }

                        String chuangwei=jsonObject2.getString("bedNo");
                        String fangjianhao=jsonObject2.getString("roomNo");
                        SPUtil.putString(SanresultActivity.this,"pid",String.valueOf(jsonObject3.getInt("pid")));
                        hzchuangwei.setText(chuangwei);
                        hzroomname.setText(fangjianhao);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }



                }
            });
        }


        startceliang.setOnClickListener(new View.OnClickListener() {//开始测量
            @Override
            public void onClick(View view) {
                                if (!getPackageManager().hasSystemFeature(
                        PackageManager.FEATURE_BLUETOOTH_LE)) {
                    Toast.makeText(SanresultActivity.this, R.string.ble_not_supported, Toast.LENGTH_SHORT)
                            .show();
                } else {
                    if (!mBluetoothAdapter.isEnabled()) {
                        Intent enableBtIntent = new Intent(
                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, 101);
                    } else {
                        //打开血压计测量血压以及连接蓝牙状态
                        Intent intent = new Intent(SanresultActivity.this, BloodPressMearActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
